#include "can.h"
#include <stdio.h>

FlagStatus can0_receive_flag;
FlagStatus can0_error_flag;
can_parameter_struct can_init_parameter;
can_filter_parameter_struct can_filter_parameter;
can_trasnmit_message_struct g_transmit_message;
can_receive_message_struct g_receive_message;

/*!
    \brief      initialize CAN and filter
    \param[in]  can_parameter
      \arg        can_parameter_struct
    \param[in]  can_filter
      \arg        can_filter_parameter_struct
    \param[out] none
    \retval     none
*/
void can_config(can_parameter_struct can_parameter, can_filter_parameter_struct can_filter)
{
    can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
    can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
    /* initialize CAN register */
    can_deinit(CAN0);
    
    /* initialize CAN parameters */
    can_parameter.time_triggered = DISABLE;
    can_parameter.auto_bus_off_recovery = DISABLE;
    can_parameter.auto_wake_up = DISABLE;
    can_parameter.auto_retrans = DISABLE;
    can_parameter.rec_fifo_overwrite = DISABLE;
    can_parameter.trans_fifo_order = DISABLE;
    can_parameter.working_mode = CAN_NORMAL_MODE;
    can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
    can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
    can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
    
    /* 1MBps */
#if CAN_BAUDRATE == 1000
    can_parameter.prescaler = 6;
    /* 500KBps */
#elif CAN_BAUDRATE == 500
    can_parameter.prescaler = 12;
    /* 250KBps */
#elif CAN_BAUDRATE == 250
    can_parameter.prescaler = 24;
    /* 125KBps */
#elif CAN_BAUDRATE == 125
    can_parameter.prescaler = 48;
    /* 100KBps */
#elif  CAN_BAUDRATE == 100
    can_parameter.prescaler = 60;
    /* 50KBps */
#elif  CAN_BAUDRATE == 50
    can_parameter.prescaler = 120;
    /* 20KBps */
#elif  CAN_BAUDRATE == 20
    can_parameter.prescaler = 300;
#else
    #error "please select list can baudrate in private defines in main.c "
#endif  
    /* initialize CAN */
    can_init(CAN0, &can_parameter);
    
    /* initialize filter */ 
    can_filter.filter_number=0;
    can_filter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high = 0x0000;
    can_filter.filter_list_low = 0x0000;
    can_filter.filter_mask_high = 0x0000;
    can_filter.filter_mask_low = 0x0000;
    can_filter.filter_fifo_number = CAN_FIFO0;
    can_filter.filter_enable = ENABLE;
    
    can_filter_init(&can_filter);
    
    /* CAN1 filter number */
    can_filter.filter_number = 15;
    can_filter_init(&can_filter);
}

// /*!
//     \brief      main function
//     \param[in]  none
//     \param[out] none
//     \retval     none
// */
// int main(void)
// {
//     uint8_t i = 0U;
    
//     /* configure GPIO */
//     can_gpio_config();
    
//     /* configure NVIC */
//     nvic_config();
//     printf("\r\nGD32F10x dual CAN test, please press Wakeup key or Tamper key to start communication!\r\n");
    
//     /* initialize CAN and filter */
//     can_config(can_init_parameter, can_filter_parameter);
//     /* enable can receive FIFO0 not empty interrupt */
//     can_interrupt_enable(CAN0, CAN_INT_RFNE0);
    
//     /* initialize transmit message */
//     g_transmit_message.tx_sfid = 0x7ab;
//     g_transmit_message.tx_efid = 0x00;
//     g_transmit_message.tx_ft = CAN_FT_DATA;
//     g_transmit_message.tx_ff = CAN_FF_STANDARD;
//     g_transmit_message.tx_dlen = 8;
    
//     g_transmit_message.tx_data[0] = 0xA0U;
//     g_transmit_message.tx_data[1] = 0xA1U;
//     g_transmit_message.tx_data[2] = 0xA2U;
//     g_transmit_message.tx_data[3] = 0xA3U;
//     g_transmit_message.tx_data[4] = 0xA4U;
//     g_transmit_message.tx_data[5] = 0xA5U;
//     g_transmit_message.tx_data[6] = 0xA6U;
//     g_transmit_message.tx_data[7] = 0xA7U;

//     while(1){
//         /* test whether the Tamper key is pressed */
//         if(0 == gd_eval_key_state_get(KEY_TAMPER)){
//             g_transmit_message.tx_data[0] = 0x55;
//             g_transmit_message.tx_data[1] = 0xAA;

//             printf("\r\n can0 transmit data:");
//             for(i = 0; i < g_transmit_message.tx_dlen; i++){
//                 printf(" %02x", g_transmit_message.tx_data[i]);
//             }
//             /* transmit message */
//             can_message_transmit(CAN0, &g_transmit_message);
//             /* waiting for the Tamper key up */
//             while(0 == gd_eval_key_state_get(KEY_TAMPER));
//         }
//         /* test whether the Wakeup key is pressed */
//         if(0 == gd_eval_key_state_get(KEY_WAKEUP)){
//             g_transmit_message.tx_data[0] = 0xAA;
//             g_transmit_message.tx_data[1] = 0x55;
//             printf("\r\n can1 transmit data:");
//             for(i = 0; i < g_transmit_message.tx_dlen; i++){
//                 printf(" %02x", g_transmit_message.tx_data[i]);
//             }
//             /* transmit message */
//             can_message_transmit(CAN1, &g_transmit_message);
//             /* waiting for the Wakeup key up */
//             while(0 == gd_eval_key_state_get(KEY_WAKEUP));
//         }
//         /* CAN0 receive data correctly, the received data is printed */
//         if(SET == can0_receive_flag){
//             can0_receive_flag = RESET;
//             printf("\r\n can0 receive data:");
//             for(i = 0; i < g_receive_message.rx_dlen; i++){
//                 printf(" %02x", g_receive_message.rx_data[i]);
//             }

//             gd_eval_led_toggle(LED4);
//         }
//         /* CAN1 receive data correctly, the received data is printed */
//         if(SET == can1_receive_flag){
//             can1_receive_flag = RESET;
//             gd_eval_led_toggle(LED5);
//             printf("\r\n can1 receive data:");
//             for(i = 0; i < g_receive_message.rx_dlen; i++){
//                 printf(" %02x", g_receive_message.rx_data[i]);
//             }
//         }
//         /* CAN0 error */
//         if(SET == can0_error_flag){
//             can0_error_flag = RESET;
//             printf("\r\n can0 communication error");
//         }
//         /* CAN1 error */
//         if(SET == can1_error_flag){
//             can1_error_flag = RESET;
//             printf("\r\n can1 communication error");
//         }
//     }
// }
